var billObj;

getBillList(1, 0, 0, "");

function getBillList(page, proId, isPayment ,proName) {
//获取总页数
	let totalPage = $("#totalPage").html();
	if (Number(totalPage)===0){
		totalPage=1
	}
	//判断页码是否正确
	if (page < 1 || page > Number(totalPage)) {
		alert("请选择正确的操作");
	} else {
		loadBillList(page, proId, isPayment ,proName);
	}
}
function erro() {
	alert("系统异常js")
}
function loadBillList(page, proId, isPayment ,proName) {
	//发送请求
	$.get("bill/billList?currentPage=" + page + "&proId=" + proId + "&isPayment=" + isPayment +"&proName=" + proName, function (data) {
		if (data.code === 200) {
			var billList = $("#billList");
			//清空表单内容
			billList.empty();
			let Payment = "未支付";
			// let queryProviderId = $("#queryProviderId");
			// let queryIsPayment = $("#queryIsPayment");
			for (let i = 0; i < data.data.list.length; i++) {
				var bill = data.data.list[i];
				if (bill.isPayment === 2) {
					Payment = "已支付"
				}
				// class="viewUser"id
				// let qpi = "<option value='" + bill.providerId + "'>" + bill.proName + "</option>";
				// let qip2="<option value='" + bill.isPayment + "'>" + arr[2] + "</option>";
				// queryIsPayment.append(qip2)
				// queryProviderId.append(qpi)
				let str =
					"<tr>" +
					"<td><span>" + bill.billCode + "</span></td>" +
					"<td><span>" + bill.productName + "</span></td>" +
					"<td><span>" + bill.proName+ "</span></td>" +
					"<td><span>" + bill.totalPrice + "</span></td>" +
					"<td><span>" + Payment + "</span></td>" +
					"<td><span>" + bill.creationDate + "</span></td>" +
					"<td><span><a class='viewUser' href='statics/jsp/bill/billview.jsp?billId="+bill.id+"'><img src='statics/images/read.png' alt='查看' title='查看'/></a></span></td>"+
					"<td><span><a class='viewUser' href='statics/jsp/bill/billmodify.jsp?billId=" + bill.id + "'><img src='statics/images/xiugai.png' alt='修改' title='修改'/></a></a></span></td>"+
					"<td><span><a class='viewUser' href='javascript:deleteBill("+bill.id+")'><img src='statics/images/schu.png' alt='删除' title='删除'/></a></span></td>"+
					"</tr>"
				billList.append(str)
			}
			//设置总条数
			$("#total").html(data.data.total);
			//设置当前页
			$("#currentPage").html(data.data.pageNum);
			//设置总页码
			$("#totalPage").html(data.data.pages);
		} else {
			alert("出问题了。")
		}
	})
}
function deleteBill(billId) {
	if (confirm("确认删除？")) {
		//发送请求
		$.ajax({
			type: "DELETE",
			url: "bill/" + billId,
			success(data) {
				if (data.code === 200) {
					alert("删除成功")
					location.href = "statics/jsp/bill/billlist.jsp"
				} else {
					alert(data.msg);
				}
			},
			error: function () {
				alert("系统异常11111")
			}
		})
	}
}

$.get("Provider/getproList",function (data) {
	if (data.code===200){
		var queryProviderId = $("#queryProviderId");
		for (let i = 0; i < data.data.length ; i++) {
			var Provider =data.data[i];
			let str="<option value='"+Provider.id+"'>"+Provider.proName+"</option>";
			queryProviderId.append(str);
		}
	}else {
		alert(data.msg);
	}
})




































//订单管理页面上点击删除按钮弹出删除框(billlist.jsp)
// function deleteBill(obj){
// 	$.ajax({
// 		type:"GET",
// 		url:path+"/jsp/bill.do",
// 		data:{method:"delbill",billid:obj.attr("billid")},
// 		dataType:"json",
// 		success:function(data){
// 			if(data.delResult == "true"){//删除成功：移除删除行
// 				cancleBtn();
// 				obj.parents("tr").remove();
// 			}else if(data.delResult == "false"){//删除失败
// 				//alert("对不起，删除订单【"+obj.attr("billcc")+"】失败");
// 				changeDLGContent("对不起，删除订单【"+obj.attr("billcc")+"】失败");
// 			}else if(data.delResult == "notexist"){
// 				//alert("对不起，订单【"+obj.attr("billcc")+"】不存在");
// 				changeDLGContent("对不起，订单【"+obj.attr("billcc")+"】不存在");
// 			}
// 		},
// 		error:function(data){
// 			alert("对不起，删除失败");
// 		}
// 	});
// }

function openYesOrNoDLG(){
	$('.zhezhao').css('display', 'block');
	$('#removeBi').fadeIn();
}

function cancleBtn(){
	$('.zhezhao').css('display', 'none');
	$('#removeBi').fadeOut();
}
function changeDLGContent(contentStr){
	var p = $(".removeMain").find("p");
	p.html(contentStr);
}

$(function(){
	$(".viewBill").on("click",function(){
		//将被绑定的元素（a）转换成jquery对象，可以使用jquery方法
		var obj = $(this);
		window.location.href=path+"/jsp/bill.do?method=view&billid="+ obj.attr("billid");
	});
	
	$(".modifyBill").on("click",function(){
		var obj = $(this);
		window.location.href=path+"/jsp/bill.do?method=modify&billid="+ obj.attr("billid");
	});
	$('#no').click(function () {
		cancleBtn();
	});
	
	$('#yes').click(function () {
		deleteBill(billObj);
	});

	$(".deleteBill").on("click",function(){
		billObj = $(this);
		changeDLGContent("你确定要删除订单【"+billObj.attr("billcc")+"】吗？");
		openYesOrNoDLG();
	});
	
	/*$(".deleteBill").on("click",function(){
		var obj = $(this);
		if(confirm("你确定要删除订单【"+obj.attr("billcc")+"】吗？")){
			$.ajax({
				type:"GET",
				url:path+"/bill.do",
				data:{method:"delbill",billid:obj.attr("billid")},
				dataType:"json",
				success:function(data){
					if(data.delResult == "true"){//删除成功：移除删除行
						alert("删除成功");
						obj.parents("tr").remove();
					}else if(data.delResult == "false"){//删除失败
						alert("对不起，删除订单【"+obj.attr("billcc")+"】失败");
					}else if(data.delResult == "notexist"){
						alert("对不起，订单【"+obj.attr("billcc")+"】不存在");
					}
				},
				error:function(data){
					alert("对不起，删除失败");
				}
			});
		}
	});*/
});